{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numpy & Pandas 学习复习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 2. 3.]\n",
      " [2. 3. 4.]]\n",
      "float64\n",
      "number of dim: 2 \n",
      "shape: (2, 3)\n",
      "size: 6\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 数组的基本定义\n",
    "array = np.array([[1, 2, 3],\n",
    "                [2, 3, 4]], dtype=np.float64) # 2行3列的数组\n",
    "\n",
    "print(array)\n",
    "print(array.dtype) # 数组的数据类型\n",
    "print(f\"number of dim: {array.ndim} \") # 数组的维度\n",
    "print(f\"shape: {array.shape}\") # 数组的形状\n",
    "print(f\"size: {array.size}\") # 数组的大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]]\n",
      "\n",
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]]\n",
      "\n",
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]]\n",
      "\n",
      "[[ 1.  ]\n",
      " [ 3.25]\n",
      " [ 5.5 ]\n",
      " [ 7.75]\n",
      " [10.  ]]\n",
      "\n",
      "[[6.32104767e-01 1.50781927e-01 7.28533598e-04]\n",
      " [4.16627833e-01 4.77364971e-01 7.41267489e-05]]\n"
     ]
    }
   ],
   "source": [
    "# 全0矩阵\n",
    "zero_array = np.zeros((2, 3))\n",
    "print(zero_array)\n",
    "\n",
    "print()\n",
    "\n",
    "# 全1矩阵\n",
    "one_array = np.ones((3, 4))\n",
    "print(one_array)\n",
    "\n",
    "print()\n",
    "\n",
    "# 有序数列\n",
    "ordered_array = np.arange(1, 11).reshape((2, 5)) # 1到10的有序矩阵，步长为2 arrrange左包括 右不包括\n",
    "print(ordered_array) # 1到10的有序矩阵，步长为2\n",
    "\n",
    "print()\n",
    "\n",
    "# 线段\n",
    "line_array = np.linspace(1, 10, 5).reshape((5, 1)) # 1到10的线段，包含5个数\n",
    "print(line_array)\n",
    "\n",
    "print()\n",
    "\n",
    "# 随机数\n",
    "random_array = np.random.rand(2, 3) # 2行3列的随机数矩阵\n",
    "print(random_array)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 20 30 50] [0 1 2 3]\n",
      "相减\n",
      "[10 19 28 47]\n",
      "相加\n",
      "[10 21 32 53]\n",
      "次方\n",
      "[ 100  400  900 2500]\n",
      "平方根\n",
      "[3.16227766 4.47213595 5.47722558 7.07106781]\n",
      "三角函数\n",
      "[-0.54402111  0.91294525 -0.98803162 -0.26237485]\n",
      "[-0.83907153  0.40808206  0.15425145  0.96496603]\n",
      "[ True False False False]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([10, 20, 30 , 50])\n",
    "b = np.arange(4)\n",
    "\n",
    "print(a, b)\n",
    "\n",
    "print(\"相减\")\n",
    "c = a - b\n",
    "print(c) # 数组之间的运算\n",
    "\n",
    "print(\"相加\")\n",
    "c = a + b\n",
    "print(c) # 数组之间的运算\n",
    "\n",
    "print(\"次方\")\n",
    "c = a**2\n",
    "print(c)\n",
    "\n",
    "print(\"平方根\")\n",
    "c = np.sqrt(a)\n",
    "print(c)\n",
    "\n",
    "print(\"三角函数\")\n",
    "c = np.sin(a)\n",
    "print(c)\n",
    "c = np.cos(a)\n",
    "print(c)\n",
    "\n",
    "print(a < 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]] \n",
      " [[0 1]\n",
      " [2 3]]\n",
      "直接相乘\n",
      "[[ 0  2]\n",
      " [ 6 12]]\n",
      "点乘\n",
      "[[ 4  7]\n",
      " [ 8 15]]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "a = np.array([[1, 2],\n",
    "              [3, 4]])\n",
    "b = np.arange(4).reshape((2, 2))\n",
    "\n",
    "print(a, \"\\n\", b)\n",
    "\n",
    "c = a * b\n",
    "c_dot = np.dot(a, b) # c_dot = a.dot(b) # 点乘\n",
    "print(\"直接相乘\")\n",
    "print(c)\n",
    "print(\"点乘\")\n",
    "print(c_dot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.9809617  0.30334362 0.70024093 0.78606767]\n",
      " [0.38562927 0.90474086 0.00739573 0.14205308]]\n",
      "[2.77061392 1.43981895]\n",
      "[0.9809617  0.90474086]\n",
      "[0.30334362 0.00739573]\n"
     ]
    }
   ],
   "source": [
    "a = np.random.random((2, 4))\n",
    "print(a)\n",
    "\n",
    "print(np.sum(a, axis=1))\n",
    "print(np.max(a, axis=1))\n",
    "print(np.min(a, axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]]\n",
      "11\n",
      "7.5\n",
      "[[1 1 1]\n",
      " [1 1 1]\n",
      " [1 1 1]]\n",
      "[[ 5  5  5  5]\n",
      " [ 6  7  8  9]\n",
      " [10 10 10 10]]\n",
      "[[ 2  6 10]\n",
      " [ 3  7 11]\n",
      " [ 4  8 12]\n",
      " [ 5  9 13]]\n"
     ]
    }
   ],
   "source": [
    "A = np.arange(2, 14).reshape((3, 4))\n",
    "print(A)\n",
    "\n",
    "print(np.argmax(A)) # 返回最大值的索引\n",
    "print(np.mean(A)) # 返回均值\n",
    "print(np.diff(A)) # 返回相邻元素的差\n",
    "print(np.clip(A, 5, 10)) # 将小于5的数替换为5，大于10的数替换为10\n",
    "print(np.transpose(A)) # 转置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Numpy索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A:\n",
      "[ 2  4  6  8 10 12]\n",
      "前三个\n",
      "[2 4 6]\n",
      "第四个\n",
      "8\n",
      "\n",
      "A:\n",
      "[[ 2  4]\n",
      " [ 6  8]\n",
      " [10 12]]\n",
      "取第二行\n",
      "[6 8]\n",
      "取第二行第一列\n",
      "6\n",
      "[8]\n",
      "<built-in method flatten of numpy.ndarray object at 0x00000187F13D0750>\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n",
      "12\n"
     ]
    }
   ],
   "source": [
    "A = np.arange(2, 14, 2)\n",
    "print(f\"A:\\n{A}\")\n",
    "print(\"前三个\")\n",
    "print(A[:3]) # 取前三个数\n",
    "print(\"第四个\")\n",
    "print(A[3])\n",
    "\n",
    "print()\n",
    "\n",
    "A = np.arange(2, 14, 2).reshape((3, 2))\n",
    "print(f\"A:\\n{A}\")\n",
    "print(\"取第二行\")\n",
    "print(A[1]) # 取第二行\n",
    "print(\"取第二行第一列\")\n",
    "print(A[1, 0]) # 取第二行第一列的数\n",
    "print(A[1, 1:])\n",
    "\n",
    "print(A.flatten) # 将多维数组转化为一维数组\n",
    "for i in A.flatten():\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Numpy array合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "A = np.array([1, 2, 3])\n",
    "B = np.array([4, 5, 6])\n",
    "\n",
    "print(np.vstack((A, B))) # 纵向堆叠\n",
    "print(np.hstack((A, B))) # 横向堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n",
      "[[1]\n",
      " [2]\n",
      " [3]\n",
      " [4]\n",
      " [5]\n",
      " [6]]\n",
      "\n",
      "[[1]\n",
      " [2]\n",
      " [3]\n",
      " [4]\n",
      " [5]\n",
      " [6]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "\n",
      "[[1 4 1]\n",
      " [2 5 2]\n",
      " [3 6 3]]\n"
     ]
    }
   ],
   "source": [
    "C = A.reshape((3, 1))\n",
    "D = B.reshape((3, 1))\n",
    "print(np.hstack((C, D))) # 横向堆叠\n",
    "print(np.vstack((C, D))) # 纵向堆叠\n",
    "print()\n",
    "print(np.concatenate((C, D, C), axis=0))\n",
    "print()\n",
    "print(np.concatenate((C, D, C), axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Numpy array分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "A = np.arange(12).reshape((3, 4))\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1],\n",
      "       [4, 5],\n",
      "       [8, 9]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11]])]\n",
      "[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]\n"
     ]
    }
   ],
   "source": [
    "print(np.split(A, 2, axis=1)) # 在行上进行分割 别列给分开\n",
    "print(np.split(A, 3, axis=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Numpy copy & deepcopy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "A = np.arange(12).reshape((3, 4))\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "[[100   1   2   3]\n",
      " [  4   5   6   7]\n",
      " [  8   9  10  11]]\n",
      "B随着A的改变 也进行了相应的改变\n",
      "[[100   1   2   3]\n",
      " [  4   5   6   7]\n",
      " [  8   9  10  11]]\n"
     ]
    }
   ],
   "source": [
    "B = A\n",
    "print(B is A)\n",
    "\n",
    "A[0, 0] = 100\n",
    "print(A)\n",
    "print(\"B随着A的改变 也进行了相应的改变\")\n",
    "print(B)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "A = np.arange(12).reshape((3, 4))\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "[[100   1   2   3]\n",
      " [  4   5   6   7]\n",
      " [  8   9  10  11]]\n",
      "B没有随着A的改变而改变\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "B = A.copy() # 复制\n",
    "print(B is A)\n",
    "\n",
    "A[0, 0] = 100\n",
    "print(A)\n",
    "print(\"B没有随着A的改变而改变\")\n",
    "print(B)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
